package com.zjuh.taofood.biz.similar;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.sql.DataSource;

import wint.tools.similar.content.Content;
import wint.tools.similar.content.ContentResource;
import wint.tools.util.CloseableIterator;

import com.zjuh.taofood.biz.dao.TaobaoCatDAO;
import com.zjuh.taofood.biz.dao.TaobaoItemDescriptionDAO;

public class TaobaoItemContentResource implements ContentResource {

	private DataSource dataSource;
	
	private TaobaoCatDAO taobaoCatDAO;
	
	private TaobaoItemDescriptionDAO taobaoItemDescriptionDAO;
	
	@Override
	public CloseableIterator<Content> getContents(String groupId) {
		try {
			Connection conn = dataSource.getConnection();
			String sql = "select id, item_id, cid, item_title from taofood_taobao_item where deleted = 0";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			rs.setFetchSize(1);
			return new TaoItemSqlResultCloseableIterator(rs, pstmt, conn, taobaoCatDAO, taobaoItemDescriptionDAO);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public void setTaobaoCatDAO(TaobaoCatDAO taobaoCatDAO) {
		this.taobaoCatDAO = taobaoCatDAO;
	}

	public void setTaobaoItemDescriptionDAO(TaobaoItemDescriptionDAO taobaoItemDescriptionDAO) {
		this.taobaoItemDescriptionDAO = taobaoItemDescriptionDAO;
	}

}
